/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.model.actor;

import java.util.Collection;

import jmmos.gameserver.model.actor.instance.MMOPcInstance;
import jmmos.gameserver.model.actor.templates.MMOCharTemplate;
import jmmos.gameserver.model.actor.templates.MMONpcTemplate;
import jmmos.gameserver.model.items.MMOWeapon;
import jmmos.gameserver.model.items.instance.MMOItemInstance;
import jmmos.gameserver.network.SystemMessageId;
import jmmos.gameserver.network.serverpackets.CharInfo;
import jmmos.gameserver.network.serverpackets.MMOGameServerPacket;
import jmmos.gameserver.taskmanager.DecayTaskManager;

public abstract class MMODecoy extends MMOCharacter
{
	private final MMOPcInstance _owner;
	
	public MMODecoy(int objectId, MMOCharTemplate template, MMOPcInstance owner)
	{
		super(objectId, template);
		setInstanceType(InstanceType.MMODecoy);
		_owner = owner;
		setXYZInvisible(owner.getX(), owner.getY(), owner.getZ());
		setIsInvul(false);
	}
	
	@Override
	public void onSpawn()
	{
		super.onSpawn();
		sendPacket(new CharInfo(this));
	}
	
	@Override
	public void updateAbnormalEffect()
	{
		Collection<MMOPcInstance> plrs = getKnownList().getKnownPlayers().values();
		
		for (MMOPcInstance player : plrs)
		{
			if (player != null)
			{
				player.sendPacket(new CharInfo(this));
			}
		}
	}
	
	public void stopDecay()
	{
		DecayTaskManager.getInstance().cancelDecayTask(this);
	}
	
	@Override
	public void onDecay()
	{
		deleteMe(_owner);
	}
	
	@Override
	public boolean isAutoAttackable(MMOCharacter attacker)
	{
		return _owner.isAutoAttackable(attacker);
	}
	
	@Override
	public MMOItemInstance getActiveWeaponInstance()
	{
		return null;
	}
	
	@Override
	public MMOWeapon getActiveWeaponItem()
	{
		return null;
	}
	
	@Override
	public MMOItemInstance getSecondaryWeaponInstance()
	{
		return null;
	}
	
	@Override
	public MMOWeapon getSecondaryWeaponItem()
	{
		return null;
	}
	
	public final int getNpcId()
	{
		return getTemplate().getNpcId();
	}
	
	@Override
	public int getLevel()
	{
		return getTemplate().getLevel();
	}
	
	public void deleteMe(MMOPcInstance owner)
	{
		decayMe();
		getKnownList().removeAllKnownObjects();
		owner.setDecoy(null);
	}
	
	public synchronized void unSummon(MMOPcInstance owner)
	{
		
		if (isVisible() && !isDead())
		{
			if (getWorldRegion() != null)
			{
				getWorldRegion().removeFromZones(this);
			}
			owner.setDecoy(null);
			decayMe();
			getKnownList().removeAllKnownObjects();
		}
	}
	
	public final MMOPcInstance getOwner()
	{
		return _owner;
	}
	
	@Override
	public MMOPcInstance getActingPlayer()
	{
		return _owner;
	}
	
	@Override
	public MMONpcTemplate getTemplate()
	{
		return (MMONpcTemplate) super.getTemplate();
	}
	
	@Override
	public void sendInfo(MMOPcInstance activeChar)
	{
		activeChar.sendPacket(new CharInfo(this));
	}
	
	@Override
	public void sendPacket(MMOGameServerPacket mov)
	{
		if (getOwner() != null)
		{
			getOwner().sendPacket(mov);
		}
	}
	
	@Override
	public void sendPacket(SystemMessageId id)
	{
		if (getOwner() != null)
		{
			getOwner().sendPacket(id);
		}
	}
}
